package me.alex.koulingbao.ui.adapter;

import android.text.format.Time;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.kongzue.dbv3.data.DBData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import me.alex.koulingbao.R;
import me.alex.koulingbao.base.BaseHolder;
import me.alex.koulingbao.base.DefaultAdapter;

/**
 * Created by Alex on 2020/10/22 0022
 * 当前adpter说明:
 */
public class KouLingAdapter extends DefaultAdapter<DBData> {

    public KouLingAdapter(List<DBData> infos) {
        super(infos);
    }

    @NonNull
    @Override
    public BaseHolder<DBData> getHolder(@NonNull View v, int viewType) {
        return new KouLingHolder(v);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.holder_kouling;
    }

    public static class KouLingHolder extends BaseHolder<DBData> {
        private ImageView intoImg, intoUsed;
        private TextView intoTitle;
        private TextView intoContent;
        private TextView intoGo;


        public KouLingHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData(@NonNull DBData data, int position) {
            intoImg = itemView.findViewById(R.id.into_img);
            intoUsed = itemView.findViewById(R.id.into_used);
            intoTitle = itemView.findViewById(R.id.into_title);
            intoContent = itemView.findViewById(R.id.into_content);
            intoGo = itemView.findViewById(R.id.into_go);
            switch (data.getString("type")) {
                case "com.taobao.taobao":
                    intoImg.setBackgroundResource(R.mipmap.taobao);
                    break;
                case "com.xunmeng.pinduoduo":
                    intoImg.setBackgroundResource(R.mipmap.pdd);
                    break;
                case "com.eg.android.AlipayGphone":
                    intoImg.setBackgroundResource(R.mipmap.alipay);
                    break;
            }
            intoTitle.setText(data.getString("title"));
            intoContent.setText(data.getString("kouling"));
            intoContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnViewClickListener.onViewClick(intoContent, position);
                }
            });
            intoGo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnViewClickListener.onViewClick(intoGo, position);
                }
            });
            if (isToday(data.getLong("date", 0))) {
                intoUsed.setVisibility(View.VISIBLE);
            } else {
                intoUsed.setVisibility(View.INVISIBLE);
            }
        }
    }

    public static boolean isToday(long when) {
        Time time = new Time("GTM+8");
        time.set(when);

        int thenYear = time.year;
        int thenMonth = time.month;
        int thenMonthDay = time.monthDay;

        time.set(System.currentTimeMillis());
        return (thenYear == time.year)
                && (thenMonth == time.month)
                && (thenMonthDay == time.monthDay);
    }
}
